package com.yoke.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by jiangzeyin on 2017/5/18.
 */
public class DateUtil {

    public static String getDate() {
        return getDate(null);
    }

    public static String getDate(String pattern) {
        if (pattern == null || pattern.length() == 0)
            pattern = "yyyyMMdd";
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(new Date());
    }

    public static int formatTime(String pattern, String time) {
        if (pattern == null || pattern.length() == 0)
            pattern = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date date;
        try {
            date = format.parse(time);
        } catch (Exception e) {
            return 0;
        }
        return (int) (date.getTime() / 1000L);
    }


    /**
     * @param pattern
     * @param date
     * @return
     * @author jiangzeyin
     * @date 2016-8-12
     */
    public static String formatTimeStamp(String pattern, long date) {
        if (pattern == null || pattern.length() == 0)
            pattern = "yyyy-MM-dd HH:mm:ss";
        Calendar nowDate = new GregorianCalendar();
        nowDate.setTimeInMillis(date * 1000L);
        DateFormat df = new SimpleDateFormat(pattern);
        return df.format(nowDate.getTime());
    }
}
